package com.craftplay.dao;

import com.craftplay.model.Collection;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface CollectionMapper {
    @Delete({
            "delete from collection",
            "where collectionId = #{collectionId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer collectionId);

    @Insert({
            "insert into collection (collectionId, userId, ",
            "unitId, createTime)",
            "values (#{collectionId,jdbcType=INTEGER}, #{userId,jdbcType=BIGINT}, ",
            "#{unitId,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP})"
    })
    int insert(Collection record);

    @InsertProvider(type = CollectionSqlProvider.class, method = "insertSelective")
    int insertSelective(Collection record);

    @Select({
            "select",
            "collectionId, userId, unitId, createTime",
            "from collection",
            "where collectionId = #{collectionId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "collectionId", property = "collectionId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.INTEGER)
    })
    Collection selectByPrimaryKey(Integer collectionId);

    @UpdateProvider(type = CollectionSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Collection record);

    @Update({
            "update collection",
            "set userId = #{userId,jdbcType=BIGINT},",
            "unitId = #{unitId,jdbcType=INTEGER},",
            "createTime = #{createTime,jdbcType=TIMESTAMP}",
            "where collectionId = #{collectionId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Collection record);


    @Select({
            "select count(*) from  collection",
            "where userId = #{userId,jdbcType=INTEGER}"
    })
    int selectByUserIdNum(int userId);


    @Delete({
            "delete from collection",
            "where userId = #{userId,jdbcType=INTEGER}  and unitId = #{unitId,jdbcType=INTEGER}"
    })
    int deleteByUserId(Collection collection);


    @Select({
            "select * from  collection",
            "where userId = #{userId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "collectionId", property = "collectionId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.INTEGER)
    })
    List<Collection> getMyCollection(Integer userId);

    @Select({
            "select * from  collection",
            "where userId = #{userId,jdbcType=INTEGER} and unitId=#{unitId}"
    })
    @Results({
            @Result(column = "collectionId", property = "collectionId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.INTEGER)
    })
    List<Collection> selectByUserIdAndUnitId(@Param(value = "userId") int userId, @Param(value = "unitId") int unitId);

    @Select({
            "select * from  collection",
            "where unitId = #{unitId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "collectionId", property = "collectionId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.INTEGER)
    })
    List<Collection> selectByUnitId(int unitId);
}